package com.ruoyi.web.controller.Base;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.domain.AbilityUser;
import com.ruoyi.domain.Ability_UserInfor;
import com.ruoyi.service.Ability_UserService;
import com.ruoyi.service.IAbilityUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 管理Controller
 * 
 * @author ruoyi
 * @date 2024-08-20
 */
@RestController
@RequestMapping("/web/user")
public class AbilityUserController extends BaseController
{
    @Autowired
    private IAbilityUserService abilityUserService;
    @Autowired
    Ability_UserService ability_userService;
    /**
     * 查询管理列表
     */

    @GetMapping("/list")
    public TableDataInfo list(AbilityUser abilityUser)
    {
        startPage();
        List<AbilityUser> list = abilityUserService.selectAbilityUserList(abilityUser);
        return getDataTable(list);
    }

    /**
     * 导出管理列表
     */

    @Log(title = "管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AbilityUser abilityUser)
    {
        List<AbilityUser> list = abilityUserService.selectAbilityUserList(abilityUser);
        ExcelUtil<AbilityUser> util = new ExcelUtil<AbilityUser>(AbilityUser.class);
        util.exportExcel(response, list, "管理数据");
    }

    /**
     * 获取管理详细信息
     */

    @GetMapping(value = "/{ID}")
    public AjaxResult getInfo(@PathVariable("ID") Long ID)
    {
        return success(abilityUserService.selectAbilityUserByID(ID));
    }

    /**
     * 新增管理
     */

    @Log(title = "管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AbilityUser abilityUser)
    {
        Ability_UserInfor userInfor = new Ability_UserInfor();
        this.init(abilityUser,userInfor);
        try {
            int k=ability_userService.insertUserInfor(userInfor);
          if(k==-100){
                return error("机构不存在");
            }
          if(k==-200){
              return error("服务器错误");
          }
        }catch (Exception e){
            e.printStackTrace();
            return error("用户名已经存在");
        }
        return toAjax(1);
    }

    /**
     * 修改管理
     */

    @Log(title = "管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AbilityUser abilityUser)
    {
        int k=abilityUserService.updateAbilityUser(abilityUser);
        if(k==-100){
            return error("机构不存在");
        }
        return toAjax(k);
    }

    /**
     * 封禁
     */

    @Log(title = "管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/block/{ID}")
    public AjaxResult remove(@PathVariable Long ID)
    {
        AbilityUser abilityUser=abilityUserService.selectAbilityUserByID(ID);
        if(abilityUser==null){
            return error("发生错误，封禁失败");
        }else {
        Integer k= abilityUserService.blocking(abilityUser);
        if(k==0||k==null){
            return error("发生错误，封禁失败,联系技术人员查看kong");
        } else if(k==1){
            return toAjax(1);
        }
        }
        return toAjax(1);
    }
    @DeleteMapping("/ublock/{ID}")
    public AjaxResult ublock(@PathVariable Long ID)
    {
        AbilityUser abilityUser=abilityUserService.selectAbilityUserByID(ID);
        if(abilityUser==null){
            return error("发生错误，解封失败");
        }else {
       Integer k= abilityUserService.unblocking(abilityUser);
       if(k==0||k==null){
           return error("发生错误，解封失败");
       } else if(k==1){
           return toAjax(1);
       }
        }
        return toAjax(1);
    }

    public void init(AbilityUser abilityUser,Ability_UserInfor userInfor) {
        userInfor.setAccount(abilityUser.getUAccount());
        userInfor.setContact(abilityUser.getUContact());
        userInfor.setData(abilityUser.getUdata());
        userInfor.setEmail(abilityUser.getUemail());
        userInfor.setPassword(abilityUser.getUPassword());
        userInfor.setPhone(abilityUser.getUphone());
        userInfor.setRemark(abilityUser.getRemark());
        userInfor.setDeptId(abilityUser.getIndept());
    }
}
